import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.servlet.http.HttpServletRequest;

import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hz.web.entity.CkHtmlEntity;
import com.hz.web.service.IckHtmlService;
import com.hz.web.service.impl.CkHtmlImpl;

@PropertySource("classpath:task.properties")
@Component
public class CreditChina {

	private final static String ORGINAL = "https://www.creditchina.gov.cn/";
	private static String[] hrefs = new String[5];
	private static String[] imgs = new String[5];
	
	@Resource
	private IckHtmlService service;
	
	/**
	 * 注入配置文件
	 * @return
	 */
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
		return new PropertySourcesPlaceholderConfigurer();
	} 
	
	/**
	 * 定时任务
	 */
	@Scheduled(cron = "${task.creditChina.xydt}")
	public void getXydt() {

		try {
			Document doc = Jsoup.connect(ORGINAL).validateTLSCertificates(false).get();
			
			Elements elements = doc.select("a.pic_left");
			for(int i = 0; i < elements.size(); i++) {
				hrefs[i] = elements.get(i).attr("href").replace("./", ORGINAL);
				imgs[i] = elements.get(i).select("img").attr("src").replace("./", ORGINAL);
				
				Document detail = Jsoup.connect(hrefs[i]).validateTLSCertificates(false).get();
				Element topElement = detail.select(".artical_caption_wrap").get(0);
				
				String title = topElement.select("h2").text();
				String createTime = topElement.select(".artical_info_left span").get(0).text();
				String summary = topElement.select(".artical_info_left span").get(1).text();
				String html = detail.select(".TRS_Editor").get(0).html();
				
				// 查询是否存在这条记录
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("inputVal", title);
				paramsMap.put("ckType", 8);
				int count = service.queryCkHtmlCount(paramsMap);
				
				String imgName = UUID.randomUUID().toString().replaceAll("-", "");
				CkHtmlEntity entity = new CkHtmlEntity();
				entity.setCkType(8);
				entity.setTitle(title);
				entity.setHtml(html);
				entity.setState("1");
				entity.setSummary(summary);
				entity.setCreateTime(new Date());
				entity.setImg(imgName + ".jpg");
				
				if(count == 0) {
					service.addCkHtml(entity);
					downloadImg(imgs[i], imgName);
				}
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		//System.out.println(System.getProperty("catalina.home"));
		//System.out.println(this.getClass().getClassLoader().getResource("/").getPath().replace("/WEB-INF/classes/", ""));
	}
	
	/**
	 * 下载图片
	 * @param urlStr 图片链接
	 * @param newFileName 图片名字
	 */
	private void downloadImg(String urlStr, String newFileName) {
    	
    	try {
    		
			URL url = new URL(urlStr);
			
			HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
			
			con.setConnectTimeout(30*1000);
			con.setHostnameVerifier(new HostnameVerifier() {
				
				@Override
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
			
			// 忽略ssl证书的操作
			final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

				@Override
				public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
					
				}

				@Override
				public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
					
				}

				@Override
				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
				
				} 
			};
			// 忽略ssl证书的操作
			final SSLContext sslContext = SSLContext.getInstance("SSL");
			sslContext.init(null, trustAllCerts,
					new java.security.SecureRandom());
			
			con.setSSLSocketFactory(sslContext.getSocketFactory());;
			
			InputStream is = con.getInputStream();
			
			byte[] bs = new byte[1024];
			
			int len;
			
			File path = new File(this.getClass().getClassLoader().getResource("/").getPath().replace("WEB-INF/classes/", "") + "images" + File.separator + "uploadimg" + File.separator);
			System.out.println(this.getClass().getClassLoader().getResource("/").getPath().replace("WEB-INF/classes/", "") + "images" + File.separator + "uploadimg" + File.separator);
			
			if(!path.exists()) {
				path.mkdirs();
			}
			
			OutputStream os = new FileOutputStream(path.getPath()+ File.separator + newFileName + ".jpg");
			System.out.println(path.getPath()+ File.separator + newFileName + ".jpg");
			
			while ((len = is.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
			
			os.close();  
	        is.close();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
    }


  /*  public String getXyzs(){
		Document document =Jsoup.connect("http://wap.ithome.com").get();
		Elements tagElement = document.getElementsByTag("title");
	}*/
	
	/*@Test
	public void getXyzs() throws IOException {
		
		List<CkHtmlEntity> list = getArticle(522, 1, 10, 5);
		System.out.println(list);
		
		// 插入数据库
		// ...
	}*/

	@Test
	public void testGetResult() throws Exception{
		SpiderImgs spider=new SpiderImgs("https://www.creditchina.gov.cn/xinyongyanjiu/yanjiuxinyongzhishi/");
	}
	/**
	 * 通过信用中国文章API 获取文章列表
	 * @param columnId 信用知识(522)
	 * @param page 页码
	 * @param pageSize 查询数量
	 * @param ckType columnId所对应数据库中的ckType
	 * @return
	 * @throws IOException
	 */
	private List<CkHtmlEntity> getArticle(Integer columnId, Integer page, Integer pageSize, Integer ckType) throws IOException {
		
		Response res = Jsoup.connect("https://www.creditchina.gov.cn/api/document/column_id_list?columnId="+ columnId +"&page="+ page +"&pageSize=" + pageSize)
				.validateTLSCertificates(false)
				.ignoreContentType(true)
				.execute();
		// 获取json
		String jsonString = res.body();
		
		JSONObject json = JSONObject.parseObject(jsonString);
		
		// 文章json数组
		JSONArray articleArray = (JSONArray) ((JSONObject) json.get("result")).get("results");
		
		// 存放文章实体类
		List<CkHtmlEntity> list = new ArrayList<CkHtmlEntity>(pageSize);
		for(Object article: articleArray) {
			String url = (String) ((JSONObject) article).get("docPuburl");
			String imgUrl = (String) ((JSONObject) article).get("imgUrl");
			
			// 获取新闻详细
			Document detail = Jsoup.connect(url).validateTLSCertificates(false).get();
			Element topElement = detail.select(".artical_caption_wrap").get(0);
			
			// 标题
			String title = topElement.select("h2").text();
			
			// 发布时间
			String createTime = topElement.select(".artical_info_left span").get(0).text();
			
			// 来源
			String summary = topElement.select(".artical_info_left span").get(1).text();
			
			// 内容
			String html = detail.select(".TRS_Editor").get(0).html();
			
			CkHtmlEntity entity = new CkHtmlEntity();
			entity.setCkType(ckType);
			entity.setTitle(title);
			//entity.setHtml(html);
			entity.setState("1");
			entity.setSummary(summary);
			entity.setCreateTime(new Date());
			entity.setImg(imgUrl);
			
			list.add(entity);
		}
		
		return list;
	}
}
